﻿using System;
using Entity;
using BLL;
using BLL.Access;
using System.Data;
using Web.Class;

namespace Web.Pages.YongShui._4.ChaoBiaoKZ
{
    public partial class JinRuXYNY : BasePage
    {
        #region 属性
        #endregion

        #region 变量
        SpHelper _sphelper = new SpHelper();
        #endregion

        #region 事件
        /// <summary>
        /// 页面加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BtnNext.Enabled = false;
            }
        }
        /// <summary>
        /// 进入下一年月
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnNext_Click(object sender, EventArgs e)
        {
            NextMonth();
        }
        /// <summary>
        /// 查错
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnSelectError_Click(object sender, EventArgs e)
        {
            SelectError();
        }
        #endregion

        #region 方法
        /// <summary>
        /// 查错
        /// </summary>
        private void SelectError()
        {
            if (CheckMonth())
            {
                string yd = BusinessTool.YS_GETSETTINGSVALUE(eSETTINGS_KEYNAME.WATER_Y_M.ToString());

                DataTable dtZhandian = BusinessTool.GetTextAndValue("view_as_userinfo", "ZHANDIANMC", "ZHANDIANBH",
                    null, string.Format("account='{0}'", LoginInfo.CurrentUser.UserID)).Tables[0];
                string zhandianBH = "";
                if (dtZhandian != null && dtZhandian.Rows.Count > 0)
                {
                    zhandianBH = dtZhandian.Rows[0]["VALUE"].ToString();
                }
                string errMsg = "";
                int? errCode = 0;
                if (zhandianBH.Trim().Length > 0)
                {
                    DataSet ds = _sphelper.P_YS_CB_JINRUXYNYCC(zhandianBH, Convert.ToInt32(yd.Substring(0, 4)),
                        Convert.ToInt32(yd.Substring(4, 2)), out errCode, out errMsg);
                    if (errCode >= 0)
                    {
                        txtResault.Text = "无错误信息，可以进入下一年月";
                    }
                    else
                    {
                        txtResault.Text = errMsg;

                    }
                }
                if (errCode < 0)
                {
                    BtnNext.Enabled = false;
                }
                else
                {
                    BtnNext.Enabled = true;
                    PageTools.AlertMsg(this,"可以进入下一年月");
                }
            }
            else
            {
                txtResault.Text = "抄表数据水量年月与当前水量年月不符，不允许进入下一水量年月！";
                BtnNext.Enabled = false;
            }

        }
        /// <summary>
        /// 进入下一年月
        /// </summary>
        private void NextMonth()
        {
            string errMsg = "";
            int? errCode = 0;
            string yd = BusinessTool.YS_GETSETTINGSVALUE(eSETTINGS_KEYNAME.WATER_Y_M.ToString());
            int sly = Convert.ToInt32(yd.Substring(0, 4));
            int slm = Convert.ToInt32(yd.Substring(4, 2));
            DataSet ds = _sphelper.P_YS_CB_JINRUXYNY("", sly, slm, out errCode, out errMsg);
            txtResault.Text = errMsg;
            BtnNext.Enabled = false;
        }
        /// <summary>
        /// 检验水量月
        /// </summary>
        /// <returns></returns>
        private bool CheckMonth()
        {
            string yd = BusinessTool.YS_GETSETTINGSVALUE(eSETTINGS_KEYNAME.WATER_Y_M.ToString());
            int sly = Convert.ToInt32(yd.Substring(0, 4));
            int slm = Convert.ToInt32(yd.Substring(4, 2));
            //验证当前自来水公司数据年月与当前水量年月是否匹配，不匹配时不允许进入下一水量年月
            int? zls_y = 0;
            int? zls_m = 0;
            _sphelper.P_YSCB_JRXYNY_YZ(sly, slm, out zls_y, out zls_m);
            return sly == zls_y && slm == zls_m;
            //{
            //    //txtResault.Text = "抄表数据水量年月与当前水量年月不符，不允许进入下一水量年月！";
            //    //BtnNext.Enabled = false;
            //}
        }
        #endregion
    }
}